package com.zhehekeji.opsys.api.service.validate;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.UserOrderResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TCompany;
import com.zhehekeji.opsys.api.entity.TProduct;
import com.zhehekeji.opsys.api.entity.TUser;
import com.zhehekeji.opsys.api.service.validate.args.UserOrderValidateArgs;

/**
 * @ClassName UserOrderValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-4-14   上午10:44:56
 */
@Service
public class UserOrderValidate extends ValidateService<ValidateRets<Object[]>, UserOrderValidateArgs> {

	private final static Logger logger = LoggerFactory.getLogger(UserOrderValidate.class);
	
	@Override
	public ValidateRets<Object[]> validate(UserOrderValidateArgs validateArgs)
			throws Exception {
		String tmpProductCode = validateArgs.getProductCode();
		Integer tmpPoint = validateArgs.getPoint();
		String tmpAccount = validateArgs.getAccount();
		Long tmpCpCode = validateArgs.getCpCode();
		SuperResp resp = validateArgs.getResp();
		ValidateRets <Object[]> validateRets = new ValidateRets<Object[]>();
		Object[] tmpArrayRets = new Object[2];
		if (StringUtils.isEmpty(tmpProductCode)){
			logger.error("订购失败，产品编码非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，产品编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (tmpPoint == null || tmpPoint <= 0){
			logger.error("订购失败，积分非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，积分非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (StringUtils.isEmpty(tmpAccount)){
			logger.error("订购失败，账号非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，账号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (tmpCpCode == null || tmpCpCode <= 0){
			logger.error("订购失败，cpCode非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，cpCode非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		TProduct tmpTProduct = entityDao.get(TProduct.class, "o.code=?1", new Object[]{tmpProductCode});
		if (tmpTProduct == null){
			logger.error("订购失败，产品编码不存在，产品编码非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，产品编码不存在，产品编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Integer tmpStatus = tmpTProduct.getStatus();
		if (tmpStatus == 0){
			logger.error("订购失败，产品已经停用！");
			resp.setResultCode(UserOrderResultCode.PRODUCT_HAS_BEEN_STOPPED);
			resp.setResultDesc("订购失败，产品已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		TUser tmpTUser = entityDao.get(TUser.class, "o.account=?1", new Object[]{tmpAccount});
		if (tmpTUser == null){
			logger.error("订购失败，账号不存在，账号非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，账号不存在，账号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		
		tmpStatus = tmpTUser.getStatus();
		if (tmpStatus == 0){
			logger.error("订购失败，用户已经停用！");
			resp.setResultCode(UserOrderResultCode.USER_HAS_BEEN_STOPPED);
			resp.setResultDesc("订购失败，用户已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		
		TCompany tmpTCompany = entityDao.get(TCompany.class, "o.code=?1", new Object[]{tmpCpCode});
		if (tmpTCompany == null){
			logger.error("订购失败，企业不存在，cpCode非法！");
			resp.setResultCode(UserOrderResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("订购失败，企业不存在，cpCode非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		
		tmpStatus = tmpTCompany.getStatus();
		if (tmpStatus == 0){
			logger.error("订购失败，企业已经停用！");
			resp.setResultCode(UserOrderResultCode.COMPANY_HAS_BEEN_STOPPED);
			resp.setResultDesc("订购失败，企业已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		tmpArrayRets[0] = tmpTUser;
		tmpArrayRets[1] = tmpTProduct;
		validateRets.setEntity(tmpArrayRets);
		return validateRets;
	}

}
